//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class StringRecord : WorksheetLevelRecord
    {
        private XLUnicodeString _value;

        public StringRecord(string value)
        {
            Contract.Requires(!string.IsNullOrEmpty(value));
            _value = (XLUnicodeString) value;
        }

        public StringRecord()
        {
            
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            var previous = context.PreviousRecord;
            if (previous is ArrayRecord)
                previous = ((ArrayRecord) previous).FormulaRecord;
            var f = previous as IFormulaRecord;
            if (f != null) f.FormulaResult =(string) _value;
            else Contract.Assert(false, "wrong record");
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.String; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            _value.Write(context);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            //ushort size = reader.ReadUInt16();
            //byte brbit = reader.ReadByte();
            _value.Read(context.Reader);
        }
    }
}